from flask import session, current_app, jsonify, g
# from flask import session


from info.utitls.response_code import RET


def set_rank_class(index):

   # 根据传入的下标返回不同的样式字符串
    if index == 1:

        return "first"
    elif index == 2:
        return "second"
    elif index == 3:
        return "third"
    else:
        return ""

import functools
# 使用装饰器装饰视图函数的话，视图函数的名称和一些注释会发生改变，
# 解决方案就是：@functools.wraps(view_func)
# g : 能够临时保存本次请求的内容

def user_login_data(view_func):
    @functools.wraps(view_func)
    def wrapper(*args, **kwargs):
        user_id = session.get("user_id")
        from info.models import User

        user = None

        if user_id:
            try:
                user = User.query.get(user_id)
            except Exception as e:
                current_app.logger.error(e)

                return jsonify(errno=RET.DBERR, errmsg="查询用户对象异常")
        g.user = user
        result = view_func(*args, **kwargs)

        return result


    return wrapper